package com.gcsoftware.web.controller;

import com.gcsoftware.web.beans.returnPage.AppCountBar;
import com.gcsoftware.web.beans.returnPage.Buckets;
import com.gcsoftware.web.beans.returnPage.HomeLabelData;
import com.gcsoftware.web.beans.returnPage.ProtectItem;
import com.gcsoftware.web.mapper.elasticsearch.HomeDao;
import com.gcsoftware.web.service.elasticsearch.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 首页
 * @author czp
 * date : 2018-04-10
 */
@Controller
@RequestMapping("/home")
public class HomeController extends BaseController{
    @Autowired
    private HomeService homeService;

    @Autowired
    private HomeDao homeDao;

    /**
     * 主页Label栏数据
     * @param range 时间范围
     * @return bean
     * curl http://localhost:8080/home/labelData -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/labelData", method = RequestMethod.POST)
    public HomeLabelData labelData(@RequestParam(value = "time_range", required = false) String range) {
        return homeService.labelData(range);
    }

    /**
     * 应用接口调用趋势图
     * @param range 时间范围
     * @return 图表数据
     * curl http://localhost:8080/home/apiCountLine -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/apiCountLine", method = RequestMethod.POST)
    public Object apiCountLine(@RequestParam(value = "time_range", required = false) String range) {
        return homeDao.apiCountLine(range);
    }

    /**
     * 敏感项保护次数TOP图
     * @param range 时间范围
     * @return 图表数据
     * curl http://localhost:8080/home/sensitiveItemTopBar -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/sensitiveItemTopBar", method = RequestMethod.POST)
    public Buckets[] sensitiveItemTopBar(@RequestParam(value = "time_range", required = false) String range) {
        return homeService.sensitiveItemTopBar(range, "BAR");
    }

    /**
     * 敏感项保护次数扇形图
     * @param range 时间范围
     * @return 图表数据
     * curl http://localhost:8080/home/sensitiveItemPie -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/sensitiveItemPie", method = RequestMethod.POST)
    public List<ProtectItem> sensitiveItemPie(@RequestParam(value = "time_range", required = false) String range) {
        return homeService.sensitiveItemPie(range);
    }

    /**
     * 自然月接口被调用次数柱状图
     * @param range 时间范围
     * @return 图表数据
     * curl http://localhost:8080/home/apiCountBar -X POST
     */
    @ResponseBody
    @RequestMapping(value = "/apiCountBar", method = RequestMethod.POST)
    public AppCountBar apiCountBar(@RequestParam(value = "time_range", required = false) String range) {
        return homeService.apiCountBar(range, null, null);
    }
}
